package com.wyl.wyl.testapplication.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wyl.wyl.testapplication.Api;
import com.wyl.wyl.testapplication.R;
import com.wyl.wyl.testapplication.base.BaseActivity;
import com.wyl.wyl.testapplication.bean.Pingbean;
import com.wyl.wyl.testapplication.utils.acache.ACacheUtils;
import com.wyl.wyl.testapplication.utils.request.RequestCallback;
import com.wyl.wyl.testapplication.utils.request.RequestUtils;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by es on 2019/4/25.
 */

public class SetActivity extends BaseActivity {
    private EditText ym, dk;
    private LinearLayout back;
    private Button save;
    private Call<Pingbean> call;
    private ProgressDialog dialog;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_set);
        ym = findViewById(R.id.ym);
        dk = findViewById(R.id.dk);
        back = findViewById(R.id.back);
        save = (Button) findViewById(R.id.save);
        initEvents();
        ym.setText(ACacheUtils.getDomain());
        dk.setText(ACacheUtils.getPort());
        dialog = new ProgressDialog(SetActivity.this);
    }


    private void initEvents() {
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void save() {
        if (TextUtils.isEmpty(ym.getText().toString())) {
            Toast.makeText(this, "域名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(dk.getText().toString())) {
            Toast.makeText(this, "端口号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
//        if (dialog != null) {
//            dialog.setMessage("正在加载数据...");
//            dialog.setCanceledOnTouchOutside(false);
//            dialog.show();
//        }
//
//        String url = UrlConstant.DEFAULT_HTTP + ym.getText().toString() + ":" + dk.getText().toString() +"/";
//        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).
//                addConverterFactory(GsonConverterFactory.create()).build();
//        call = retrofit.create(Api.class).pingUrl(/*ym.getText().toString() , dk.getText().toString()*/);
//        call.enqueue(new Callback<Pingbean>() {
//            @Override
//            public void onResponse(Call<Pingbean> call, Response<Pingbean> response) {
//                Log.i("aaa" , "成功");
//                if (dialog.isShowing()){
//                    dialog.dismiss();
//                }
//                if (response.body().getHead().getStatus().equals("true")) {
//                    SPUtils.saveDomain(SetActivity.this , ym.getText().toString());
//                    SPUtils.savePort(SetActivity.this,dk.getText().toString());
//                    finish();
//                }else {
//                    Toast.makeText(SetActivity.this, response.body().getHead().getMsg(), Toast.LENGTH_SHORT).show();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Pingbean> call, Throwable t) {
//                Toast.makeText(SetActivity.this, "域名端口号设置失败", Toast.LENGTH_SHORT).show();
//                Log.i("aaa" , "失败");
//                if (dialog.isShowing()){
//                    dialog.dismiss();
//                }
//            }
//        });
        RequestUtils.getRetrofit(SetActivity.this, ym.getText().toString(), dk.getText().toString()).create(Api.class).pingUrl().
                enqueue(new RequestCallback<Pingbean>(SetActivity.this) {
                    @Override
                    public void success(Response<Pingbean> response) {
                        ACacheUtils.setDomain(ym.getText().toString());
                        ACacheUtils.setPort(dk.getText().toString());
                        finish();
                    }
                });

    }
}

